package utils;

import java.io.InputStream;
import java.util.Properties;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;


public abstract class PropUtils {
	private static ResourceLoader resourceLoader = new DefaultResourceLoader();

	public final static Properties PROPERTIES = new Properties();

	static {
		loadProperties(PROPERTIES, "server.properties");
	}

	/**
	 * 载入多个文件, 文件路径使用Spring Resource格式.
	 */
	private static void loadProperties(Properties props, String baseFile) {
		InputStream is = null;
		Resource baseResource = resourceLoader.getResource(baseFile);
		
		try {
			is = baseResource.getInputStream();
			props.load(is);
		} catch (Exception ex) {
		} finally {
			IOUtils.closeQuietly(is);
		}
	}

//	private static List<Resource> readPropertieFiles(String baseFile) {
//		InputStream is = null;
//		ArrayList<Resource> resourcesPaths = new ArrayList<Resource>();
//		try {
//			Resource baseResource = resourceLoader.getResource(baseFile);
//			is = baseResource.getInputStream();
//			List<String> list = IOUtils.readLines(is);
//			for (String location : list) {
//				location = location.trim();
//				if (location.length() == 0)
//					continue;
//				if (location.startsWith(Constants.FILE_SPLIT))
//					continue;
//				if (location.startsWith(Constants.FILE_UNION))
//					continue;
//				Resource resource = getResource(location);
//				if (resource != null)
//					resourcesPaths.add(resource);
//			}
//		} catch (Exception ex) {
//			ex.printStackTrace();
//		} finally {
//			IOUtils.closeQuietly(is);
//		}
//		return resourcesPaths;
//	}

	private static Resource getResource(String name) {
		Resource resource = null;
		try {
			resource = resourceLoader.getResource(name);
			if (resource == null || !resource.exists())
				return null;
		} catch (Exception ex) {
		}
		return resource;
	}

	/**
	 * 取出Property，但以System的Property优先.
	 */
	public static String getProperty(String key, String defaultValue) {
		String result = getProperty(key);
		if (result != null) {
			return result;
		} else {
			return defaultValue;
		}
	}

	/**
	 * 取出Property，但以System的Property优先.
	 */
	public static Integer getInteger(String key) {
		return Integer.valueOf(getProperty(key));
	}

	/**
	 * 取出Property，但以System的Property优先.
	 */
	public static Integer getInteger(String key, Integer defaultValue) {
		return Integer.valueOf(getProperty(key, String.valueOf(defaultValue)));
	}

	/**
	 * 取出Property，但以System的Property优先.
	 */
	public static Boolean getBoolean(String key) {
		return Boolean.valueOf(getProperty(key));
	}

	/**
	 * 取出Property，但以System的Property优先.
	 */
	public static Boolean getBoolean(String key, boolean defaultValue) {
		return Boolean.valueOf(getProperty(key, String.valueOf(defaultValue)));
	}

	public static String getProperty(Properties prop, String key) {
		return StringUtils.trim(prop.getProperty(key));
	}

	public static String getProperty(String key) {
		return StringUtils.trim(PROPERTIES.getProperty(key));
	}
}
